package com.hhu.wangzb.server.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.hhu.wangzb.common.entity.PvDistrictEntity;
import com.hhu.wangzb.server.service.PvDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hhu.wangzb.common.entity.filter.EntityFilter;
import com.hhu.wangzb.common.entity.ProjectDetailEntity;
import com.hhu.wangzb.server.service.ProjectDetailService;
import com.hhu.wangzb.common.utils.PageUtils;
import com.hhu.wangzb.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author wangzb
 * @email 494199942@qq.com
 * @date 2021-07-03 17:37:59
 */
@RestController
public class ProjectDetailController {

    @Autowired
    private ProjectDetailService projectDetailService;

    @Autowired
    private EntityFilter entityFilter;

    @Autowired
    private PvDistrictService pvDistrictService;

    /**
     * 获取全部数据（分页数据，排序数据...）
     */
    @GetMapping("/projectDetails")
    public R list(@RequestParam Map<String, Object> params, String filter) {
        PageUtils page = projectDetailService.queryPage(params);

        page.setList((List<?>) entityFilter.filterList(page.getList(), filter));

        return R.ok().put("page", page);
    }


    /**
     * 获取某条数据
     */
    @GetMapping("/projectDetail/{id}")
    public R info(@PathVariable("id") Integer id, String filter) {
		ProjectDetailEntity projectDetail = projectDetailService.getById(id);

        Object o = entityFilter.filterEntity(projectDetail, filter);

        return R.ok().put("projectDetail", o);
    }

    @GetMapping("/children/projectDetails")
    public R getByChildrenId(HttpServletRequest request) {

        Map<String, String[]> parameterMap = request.getParameterMap();

        Map<String, Object> queryMap = new HashMap<>();
        for (String s : parameterMap.keySet()) {
            queryMap.put(s, parameterMap.get(s)[0]);
        }

        return R.ok().put("data", projectDetailService.queryByParamsMap(queryMap));
    }

    @GetMapping("/grandChildren/projectDetails")
    public R getByGrandChildrenId(HttpServletRequest request) {

        Map<String, String[]> parameterMap = request.getParameterMap();


        Map<String, Object> queryMap = new HashMap<>();
        for (String s : parameterMap.keySet()) {
            queryMap.put(s, parameterMap.get(s)[0]);
        }

        PvDistrictEntity entity = pvDistrictService.queryByParamsMap(queryMap);

        queryMap.clear();

        queryMap.put("pva_id", entity.getId());

        return R.ok().put("data", projectDetailService.queryByParamsMap(queryMap));
    }

    /**
     * 创建
     */
    @PutMapping("/projectDetail")
    public R save(@RequestBody ProjectDetailEntity projectDetail){
		projectDetailService.save(projectDetail);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/projectDetail")
    public R update(@RequestBody ProjectDetailEntity projectDetail){
		projectDetailService.updateById(projectDetail);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/projectDetail")
    public R delete(Integer[] ids){
		projectDetailService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
